package com.jinkaimen.goodcash.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.pullrefresh.PullToRefreshBase;
import com.ht.baselibs.ui.widget.pullrefresh.PullToRefreshListView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.NetUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.ReportShoperManagerBean;
import com.jinkaimen.goodcash.bean.ReportShoperManagerItemBean;
import com.jinkaimen.goodcash.constant.ConstantPool;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.ui.adapter.PassShoperManagerAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class PassShoperManagerActivity extends BaseActivity {
    private int currentPage = 1;
    private boolean isFinsh = true;
    private PullToRefreshListView pullToRefreshListView;
    private ListView mListView;
    private ArrayList<ReportShoperManagerItemBean> mDataList;
    private PassShoperManagerAdapter passShoperManagerAdapter;

    @Override
    protected boolean isStaticPage() {
        return true;
    }


    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("已通过商户");
    }


    @Override
    protected int initContentView() {
        return R.layout.activity_base_refresh_listview;
    }


    @Override
    protected void initChildView() {
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.plv_activity_base_refresh_listview);

        pullToRefreshListView.setPullLoadEnabled(false);
        pullToRefreshListView.setScrollLoadEnabled(true);
        mListView = pullToRefreshListView.getRefreshableView();
    }

    @Override
    protected void initChildData() {
        mDataList = new ArrayList<>();
        passShoperManagerAdapter = new PassShoperManagerAdapter(PassShoperManagerActivity.this, mDataList);
        mListView.setAdapter(passShoperManagerAdapter);
    }


    @Override
    protected void setChildViewListener() {
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentPage = 1;
                if (!NetUtils.isNetWorkConnection()) {
                    UIUtils.showToast("请检查您的网络设置");
                    return;
                }
                pullToRefreshListView.resetHasMore();
                currentPage = 1;
                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                currentPage++;
                load();
            }
        });
        pullToRefreshListView.doPullRefreshing(true, 800);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                if (mDataList != null && position <= mDataList.size()) {
                    bundle.putLong("report_shop_id", mDataList.get(position).id);
                    bundle.putBoolean("canEdit", false);
                    bundle.putLong("report_user_id", mDataList.get(position).uid);
                    bundle.putString("report_user_address", mDataList.get(position).address);
                    ActivityUtils.startActivity(PassShoperManagerActivity.this, RegisterNewActivity.class, bundle);
                }
            }
        });

    }


    protected void load() {
        if (isFinsh) {
            isFinsh = false;
            ApiManager.getApi().reportPassShoperList(
                    "INV_HSY001013",
                    SharedPreferencesUtils.getLong(ConstantPool.REPORT_ACCOUT_ID, 0),
                    currentPage,
                    0
            ).enqueue(new NetCallBack<ReportShoperManagerBean>() {
                @Override
                protected void onSuccess(Call<ReportShoperManagerBean> call, Response<ReportShoperManagerBean> response) {
                    isFinsh = true;

                    if (currentPage == 1) {
                        mDataList.clear();
                    }

                    if (response != null && response.body() != null) {
                        List<ReportShoperManagerItemBean> list = response.body().list;
                        mDataList.addAll(list);
                        if (currentPage != 1 && list.size() <= 0) {
                            UIUtils.showToast("没有更多内容");

                        }
                    }
                    passShoperManagerAdapter.notifyDataSetChanged();

                }

                @Override
                protected void hiddenProgress() {
                    if (currentPage == 1) {
                        pullToRefreshListView.onPullDownRefreshComplete();
                    } else {
                        pullToRefreshListView.onPullUpRefreshComplete();
                    }

                }


                @Override
                public void onFailure(Call<ReportShoperManagerBean> call, Throwable t) {
                    super.onFailure(call, t);
                    isFinsh = true;
                }
            });
        }
    }


}
